﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BestBuy.Common;

namespace BestBuy.DAL
{
    public class CMessage
    {
        private TradeKeyDataContext context = new TradeKeyDataContext();
        public CMessage() { }

        public bool InsertMessage(BMessage sms) {
            try
            {
                context.BMessages.InsertOnSubmit(sms);
                context.SubmitChanges();
            }
            catch
            {
                return false;
            }
            return true;
        }

        public bool InsertAllMessage(List<BMessage> listsms)
        {
            try
            {
                    context.BMessages.InsertAllOnSubmit(listsms.ToList());
                    context.SubmitChanges();
            }
            catch
            {
                return false;
            }
            return true;
        }

        public BMessage GetMessageExists(int idgui , int idnhan,int idcontact,int source) {
            var q = (from dl in context.BMessages
                     where dl.IDTKSender == idgui && dl.Source == source &&
                     dl.IDTKReceiver == idnhan &&
                     dl.IDContact == idcontact && dl.Status != (int)MessageStatus.Deleted
                     orderby dl.ID descending
                     select dl).FirstOrDefault();
            return q;
        }
        public static List<BMessage> getListMessage(int idUser)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            return context.BMessages.Where(p => p.IDTKReceiver == idUser && (p.Status == (int)MessageStatus.SentUnread || p.Status == (int)MessageStatus.Read)).ToList();
        }

        public static List<BMessage> getListSellOffer(int idUser)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            return context.BMessages.Where(p => p.IDTKReceiver == idUser && (p.Status == (int)MessageStatus.SentUnread || p.Status == (int)MessageStatus.Read)).ToList();
        }

        public static String getNameSender(int idsender)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            return context.ThongTinTaiKhoans.Where(p => p.IDTaiKhoan == idsender).FirstOrDefault().HoTen;
        }

        public static String getAccount(int idsender)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            return context.ThongTinTaiKhoans.Where(p => p.IDTaiKhoan == idsender).FirstOrDefault().Email;
        }

        public static String getNameCountry(int idsender)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            int idquocgia = context.ThongTinTaiKhoans.Where(p => p.IDTaiKhoan == idsender).FirstOrDefault().IDQuocGia;
            return context.QuocGias.Where(p => p.IDQuocGia == idquocgia).First().TenQuocGia;
        }
        public static int countMessage(int iduser)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            return context.BMessages.Where(p => p.Status == (int)MessageStatus.SentUnread && p.IDTKReceiver == iduser).Count();
        }

        public static int countTotalMessage(int idUser)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            return context.BMessages.Where(p => p.IDTKReceiver == idUser && (p.Status == (int)MessageStatus.SentUnread || p.Status == (int)MessageStatus.Read)).ToList().Count;
        }

        public static BMessage getMessage(int idmess)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            return context.BMessages.Where(p => p.ID == idmess).FirstOrDefault();
        }

        public static String getTitle(int idsender,int idsource, int idcontact)
        {
            String tieude = "";
            TradeKeyDataContext context = new TradeKeyDataContext();
            if (idsource == 1)
            {
                tieude = context.SanPhams.Where(p => p.IDSanPham == idcontact).FirstOrDefault().TenSanPham;
            }
            if (idsource == 2)
            {
                tieude = context.BangTinMuas.Where(p => p.IDBangTinMua == idcontact).FirstOrDefault().TieuDe;
            }
            if (idsource == 3)
            {
                tieude = context.BangTinBans.Where(p => p.IDBangTinBan == idcontact).FirstOrDefault().TieuDe;
            }
            return tieude;
        }

        public static void updateStatusMessage(int id)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            var kq = context.BMessages.Where(p => p.ID == id).FirstOrDefault();
            kq.Status = (int)MessageStatus.Read;
            context.SubmitChanges();
        }
        public static void addMessage(BMessage mess)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            context.BMessages.InsertOnSubmit(mess);
            context.SubmitChanges();
        }
        public static void eraseMessage(int idmess, int status)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            var kq = context.BMessages.Where(p => p.ID == idmess).FirstOrDefault();
            kq.Status = status;
            context.SubmitChanges();
        }
        public static List<BMessage> getListDellMess(int idUser)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            return context.BMessages.Where(p => p.IDTKReceiver == idUser && p.Status == (int)MessageStatus.Erase).ToList();
        }
        public static int countEraseMessage(int idUser)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            return context.BMessages.Where(p => p.IDTKReceiver == idUser && p.Status == (int)MessageStatus.Erase).ToList().Count();
        }
        public static int countMessageitem(int idUser)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            return context.BMessages.Where(p => p.IDTKSender == idUser).ToList().Count();
        }
        public static List<BMessage> getListSentMess(int idUser)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            return context.BMessages.Where(p => p.IDTKSender == idUser).ToList();
        }
    }
}